package com.qd.marketlist;

import java.util.ArrayList;

import android.R.color;
import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class AddItemActivity extends Activity {
	private static final String TAG = "AddItemActivity";
    private static final boolean DBG = MLConst.DEBUG;
	private Cursor mCursor;
	private ArrayAdapter mAdapter;
	private AutoCompleteTextView mActv;
	private Uri mHistoryUri;
	private Uri mUri;

	private void addToDB(Intent intent){
	    String addString = intent.getStringExtra(MLConst.KEY_ADD_STRING);

        if(addString.isEmpty()){
        }else{
            ContentValues values = new ContentValues();
            values.put(MLConst.DBSTR_COL_NAME, addString);
            values.put(MLConst.DBSTR_COL_CHECKBOX_STATE, MLConst.DB_STATE_UNCHECKED);
            this.getContentResolver().insert(mUri, values);
            if(DBG) Log.e(TAG, "add " + addString + " to " + mUri.toString());
        }
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.history_list);

        ActionBar actionBar = getActionBar();

        actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

	    ArrayList<String> columnArray1 = new ArrayList<String>();

	    String curType = getIntent().getStringExtra(MLConst.KEY_CUR_TYPE);
	    if(curType.equals(MLConst.DBSTR_TYPE_SUPERMARKET)){
	        mHistoryUri = MyProvider.CONTENT_HISTORYURI;
	        mUri = MyProvider.CONTENT_URI;
	    }else{
	        mHistoryUri = MyProvider.CONTENT_DRUGHISTORY_URI;
	        mUri = MyProvider.CONTENT_DRUG_URI;
	    }
	    if(DBG) Log.e(TAG,"onCreate()" + mHistoryUri.toString()
	            + "type= " + curType);
	    mCursor = this.getContentResolver().query(
	            mHistoryUri,
                null,
                null,
                null,
                MLConst.DBSTR_COL_COUNT + " DESC");

	    if(null != mCursor){
	        mCursor.moveToFirst();

	    	int[] rids = {
	    		R.id.first_block,
	    		R.id.second_block,
	    		R.id.third_block,
	    		R.id.forth_block,
	    		R.id.fifth_block,
	    		R.id.sixth_block,
	    		R.id.seventh_block,
	    		R.id.eighth_block,
	    		R.id.ninth_block
	    		};

	    	for(int rid:rids){
	    		TextView tv = (TextView)findViewById(rid);
	    		String s = mCursor.getString(mCursor.getColumnIndex(MLConst.DBSTR_COL_NAME));
	    		//if(DBG) Log.e(TAG, "count" + mCursor.getString(mCursor.getColumnIndex(MLConst.DBSTR_COL_COUNT)));
	    		//if(DBG) Log.e(TAG, "tv" + tv.getTag().toString());
	    		tv.setText(s);
	    		columnArray1.add(s);
	    		mCursor.moveToNext();
	    	}
	    	mActv = (AutoCompleteTextView)findViewById(R.id.add_editview);
	    	mAdapter = new ArrayAdapter(this,
	    	        android.R.layout.simple_dropdown_item_1line,
	    	        columnArray1.toArray());
	    	mActv.setAdapter(mAdapter);
	    	mActv.setImeOptions(EditorInfo.IME_ACTION_DONE);

	    }
	}

	@Override
	protected void onDestroy(){
	    super.onDestroy();
	    if(DBG) Log.e(TAG,"onDestroy()");
	    mCursor.close();
	}
	public void handleTextView(View v){
		TextView tv = (TextView)v;
		if(DBG) Log.e(TAG,"handleTextView()" + tv.getText().toString());
		Intent it = getIntent();
    	it.putExtra(MLConst.KEY_ADD_STRING, tv.getText().toString());
    	addToDB(it);
    	setResult(RESULT_OK, it);
    	finish();
	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_additem, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.menu_add:{
        	// EditText et = (EditText)findViewById(R.id.add_editview);
        	Intent it = getIntent();
        	it.putExtra(MLConst.KEY_ADD_STRING, mActv.getText().toString());
        	addToDB(it);
        	setResult(RESULT_OK, it);
        	finish();
        	break;
        }
        default:{
        	break;
        }
        }
        return true;
    }
}
